package com.lance.shiro.web;

import com.lance.shiro.entity.ISalesNotification;
import com.lance.shiro.service.SalesNotificationService;
import com.lance.shiro.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

/**
 * Created by bingyun on 2018-06-02.
 */
@RestController
@RequestMapping("/rest/saleNotification")
public class SalesNotificationController {

    @Autowired
    private SalesNotificationService salesNotificationService;

    /**
     * 查询全部
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity findAll() {
        ArrayList<Map> list = salesNotificationService.findAll();
        return RequestUtils.success("Operation success!", list);
    }

    /**
     * 根据id 查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResponseEntity get(@PathVariable("id") int id) {
        Map obj = salesNotificationService.get(id);
        return RequestUtils.success("Operation success!", obj);
    }

    /**
     * 保存数据
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity add(@RequestBody ISalesNotification entity) throws Exception {
        Map obj = salesNotificationService.saveAdd(entity);
        return RequestUtils.success("Operation success!", obj);
    }

    /**
     * 更新数据
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResponseEntity edit(@RequestBody ISalesNotification entity) throws Exception {
        if (null != entity) {
            Map obj = salesNotificationService.saveUpdate(entity);
            return RequestUtils.success("Operation success!", obj);
        } else {
            return RequestUtils.success("Operation success!", null);
        }
    }

    /**
     * 拒绝请求
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "reject/{id}", method = RequestMethod.PUT)
    public ResponseEntity reject(@PathVariable("id") int id, @RequestBody Map<String, String> reqMap) {
        Map obj = salesNotificationService.reject(id, reqMap);
        return RequestUtils.success("Operation success!", obj);
    }

    /**
     * 同意请求
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "accept/{id}", method = RequestMethod.PUT)
    public ResponseEntity accept(@PathVariable("id") int id, @RequestBody Map<String, String> reqMap) {
        Map obj = salesNotificationService.accept(id, reqMap);
        return RequestUtils.success("Operation success!", obj);
    }

    /**
     * delete
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResponseEntity delete(@RequestParam ArrayList<Integer> id) {
        salesNotificationService.deleteAllByIds(id);
        return RequestUtils.success("Operation success!");
    }

}
